package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.user;


import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.user.devicedata.DeviceDataBean;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.util.PresentationUtil;

/**
 * 
 * Este Bean es utilizado en las vistas
 * simpleuser/listdevices.xhtml
 *
 */
@ManagedBean(name="devicesListBean")
@ViewScoped
public class DevicesListBean {


	@ManagedProperty(value="#{usuarioBean}")
	private UsuarioBean userBean;

	
	private List<DeviceDataBean> deviceDataBean;

	@PostConstruct
	public void init(){
		setDeviceDataBean(DeviceDataBean.wrap(this.userBean.getUserDataService().findDevices()));
		for (DeviceDataBean serv : deviceDataBean) {
			PresentationUtil.addDeviceToCurrentView(serv.getDeviceService());
		}
	}
	
	@PreDestroy
	public void finalize(){
		setDeviceDataBean(DeviceDataBean.wrap(this.userBean.getUserDataService().findDevices()));
		for (DeviceDataBean serv : deviceDataBean) {
			PresentationUtil.removeDeviceToCurrentView(serv.getDeviceService());
		}
	}
	/**
	 * @return the userBean
	 */
	public UsuarioBean getUserBean() {
		return userBean;
	}

	/**
	 * @param userBean the userBean to set
	 */
	public void setUserBean(UsuarioBean userBean) {
		this.userBean = userBean;
	}

	
	public List<DeviceDataBean> getDevices(){
		return getDeviceDataBean();
	}

	/**
	 * @return the deviceDataBean
	 */
	public List<DeviceDataBean> getDeviceDataBean() {
		return deviceDataBean;
	}

	/**
	 * @param deviceDataBean the deviceDataBean to set
	 */
	public void setDeviceDataBean(List<DeviceDataBean> deviceDataBean) {
		this.deviceDataBean = deviceDataBean;
		
	}

	
}
